package top.anydata.lifeofcode.lifesystem.conf;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;

import java.util.HashMap;

/**
 * 功能描述:
 *
 * @author <a href="mailto:caingao@anydata.top">CainGao</a>
 * @version V_1.0
 * @date 2023/5/15 8:11 PM
 */
@Data
public class BaseResult {

    private Integer code;

    private String message;

    private Object data;

    /**
     * 调用失败,给予失败原因
     * @param restResultEnum
     * @return
     */
    public BaseResult error(RestResultEnum restResultEnum){
        this.code = restResultEnum.getCode();
        this.message = restResultEnum.getMessage();
        this.data= new HashMap<>();
        return this;
    }

    /**
     * 调用成功,给予成功数据
     * @param data
     * @return
     */
    public BaseResult success(Object data){
        this.data=data;
        this.message = RestResultEnum.OK.getMessage();
        this.code=RestResultEnum.OK.getCode();
        return this;
    }

    public String toJson(){

        return JSONObject.toJSONString(this);
    }

}
